package com.ehome.poct.terminal.service;

import cn.hutool.core.util.ObjectUtil;
import com.ehome.core.page.PageInfo;
import com.ehome.poct.base.api.feign.ClinicClient;
import com.ehome.poct.base.api.feign.DeviceClient;
import com.ehome.poct.core.api.dto.check.*;
import com.ehome.poct.core.api.dto.order.TestOrderCheckDTO;
import com.ehome.poct.core.api.dto.order.TestOrderCheckRecordDTO;
import com.ehome.poct.core.api.dto.order.TestOrderCheckRecordQry;
import com.ehome.poct.core.api.feign.*;
import com.ehome.poct.core.api.qry.OrderQry;
import com.ehome.poct.core.api.vo.check.CellBoardValidVO;
import com.ehome.poct.core.api.vo.check.CheckProcessResultVO;
import com.ehome.poct.core.api.vo.check.CheckStatusVO;
import com.ehome.poct.core.api.vo.order.TestOrderCheckRecordVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 检测服务
 *
 * @author cq
 * @since 2022/03/15
 **/
@Service
@AllArgsConstructor
public class CheckService {
    private final CheckClient checkFeginClient;
    private final ClinicClient clinicClient;
    private final DeviceClient deviceClient;
    private final OrderCheckRocordClient orderCheckRocordClient;
    private final PatientClient patientClient;
    private final CheckStripClient checkStripClient;
    private final OrderClient orderClient;
    private final ReportClient reportClient;

    public CheckProcessResultVO doCheckProcess(CheckReqDTO checkReq) {
        CheckParamDTO checkParam = new CheckParamDTO(checkReq);
        checkParam.setClinic(clinicClient.getById(checkParam.getClinicId()));
        checkParam.setDevice(deviceClient.getBySno(checkParam.getDeviceSno()));
        return checkFeginClient.doProcessCheck(checkParam);
    }


    public CheckStatusVO pollGetStatus(Long id) {
        return checkFeginClient.pollGetStatus(id);
    }

    public CheckStatusVO manualGetStatus(Long id) {
        return checkFeginClient.manualGetStatus(id);
    }

    public void algoNotify(AlgoNotifyDTO param) {
        checkFeginClient.algoNotify(param);
    }

    public CheckProcessResultVO algoNotifyLocal(AlgoLocalDataDTO param) {
        return checkFeginClient.saveLocalAlgoData(param);
    }

    public Boolean boardIsExist(String sno) {
        return checkFeginClient.boardIsExist(sno);
    }

    public CellBoardValidVO boardThickness(String sno) {
        return checkFeginClient.boardThickness(sno);
    }

    public PageInfo<TestOrderCheckRecordVO> getCheckNum(TestOrderCheckDTO testOrderCheckDTO) {
        var testOrderCheckRecordQry = new TestOrderCheckRecordQry();
        testOrderCheckRecordQry.setClinicId(testOrderCheckDTO.getClinicId());
        testOrderCheckRecordQry.setPageIndex(testOrderCheckDTO.getPageIndex());
        testOrderCheckRecordQry.setPageSize(testOrderCheckDTO.getPageSize());
        testOrderCheckRecordQry.setTestType(testOrderCheckDTO.getDeviceType());
        testOrderCheckRecordQry.setStatus(0);
        testOrderCheckRecordQry.setIsDelete(Boolean.FALSE);
        Map<String, List<String>> projectMap = new HashMap<>();
        var page = orderCheckRocordClient.list(testOrderCheckRecordQry);
        if (ObjectUtil.isNotEmpty(page.getList())){
            List<String> orderNos = page.getList().stream().map(TestOrderCheckRecordVO::getOrderNo).collect(Collectors.toList());
            List<ProjectNameReportDTO> projectNameReportDTOList = reportClient.projectNameList(orderNos);
            projectMap = projectNameReportDTOList.stream().collect(Collectors.toMap(ProjectNameReportDTO::getOrderNo, ProjectNameReportDTO::getReportName));
        }
        Map<String, List<String>> finalProjectMap = projectMap;
        page.getList().forEach(r -> {
            r.setReservationCodeString(String.format("%04d",r.getReservationCode()));
            if (r.getPatientId() != null) {
                r.setPatientDetailVO(patientClient.getPatient(r.getPatientId()));
            }
            if(r.getOrderNo() != null){
                List<String> orDefault = finalProjectMap.getOrDefault(r.getOrderNo(), new ArrayList<>());
                r.setTestItem(String.join("、",orDefault));
            }
        });
        return page;
    }

    public List<TestItemVO> testItems(String orderNo) {
        var checkRecordVO = orderCheckRocordClient.getOne(new TestOrderCheckRecordDTO().setOrderNo(orderNo).setTestType(2));
        var codes = checkRecordVO.getTestItemCode().split(",");
        var items = checkStripClient.jbyCheckInfo(1);
        List<TestItemVO> list = new ArrayList<>();
        Arrays.stream(codes).forEach(r -> {
            items.stream().filter(c -> c.getTypeCode().equals(Integer.valueOf(r))).findFirst().ifPresent(name -> list.add(new TestItemVO(name.getName().replace("检测", ""), Integer.valueOf(r), name.getImgUrl())));
        });
        return list;
    }

    public void updateCheckStatus(CheckStatusDTO checkStatusDTO) {
        //修改订单状态
        orderClient.updateByOrderNo(checkStatusDTO);
    }
}
